package com.kevin.mapping.script.node.stmt;

import com.kevin.mapping.script.engine.JSONEnv;
import com.kevin.mapping.script.exception.ExecuteException;
import com.kevin.mapping.script.lexer.Token;

public class WhileStmt extends Stmt {
    private Stmt condition;
    private Stmt stmt;

    public WhileStmt(Token token, Stmt condition, Stmt stmt) {
        super(token);
        this.condition = condition;
        this.stmt = stmt;
    }

    public Stmt getCondition() {
        return condition;
    }

    public void setCondition(Stmt condition) {
        this.condition = condition;
    }

    public Stmt getStmt() {
        return stmt;
    }

    public void setStmt(Stmt stmt) {
        this.stmt = stmt;
    }

    @Override
    public Token execute(JSONEnv env) {
        Token ret = null;
        while (true) {
            Object value = condition.getValue(env);
            if (value instanceof Boolean) {
                if ((boolean) value) {
                    ret = stmt.execute(env);
                    continue;
                }
                return ret;
            } else {
                throw new ExecuteException("unSuported type. value = " + value.toString());
            }
        }
    }
}
